import os
import sys
localDir = os.path.dirname(__file__)
curpath = os.path.normpath(os.path.join(os.getcwd(), localDir))
grandparent = os.path.normpath(os.path.join(curpath, '../'))
sys.path.insert(0, grandparent)

import unittest

from lib.change_column import ChangeColumn

from databases.mysql import Mysql
from databases.sqlite import Sqlite
from databases.db2 import Db2
from databases.mssql import Mssql
from databases.oracle import Oracle
from databases.postgres import Postgres

config = {"development" : {"db" : "sqlite:///:memory:"}}

class TestChangeColumn(unittest.TestCase):

    def testTruth(self):
        self.assertTrue(True)

    def testInitialiazeMysql(self):
        alc = Mysql(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36, default : NULL"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info MODIFY COLUMN version VARCHAR(36)  DEFAULT NULL;", sql.query)

    def testInitialiazeSqlite(self):
        alc = Sqlite(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36, default : NULL"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual(None, sql.query)

    def testInitialiazeDb2DataType(self):
        alc = Db2(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info ALTER COLUMN version SET DATA TYPE VARCHAR(36);", sql.query)

    def testInitialiazeDb2Default(self):
        alc = Db2(config['development']['db'])

        query = "schema_info, version, default : 0"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info ALTER COLUMN version SET  DEFAULT '0';", sql.query)

    def testInitialiazePostgresDataType(self):
        alc = Postgres(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info ALTER COLUMN version TYPE VARCHAR(36);", sql.query)

    def testInitialiazePostgresDefault(self):
        alc = Postgres(config['development']['db'])

        query = "schema_info, version, default : 0"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info ALTER COLUMN version SET  DEFAULT '0';", sql.query)

    def testInitialiazeOracle(self):
        alc = Oracle(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36, default : NULL"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info MODIFY ( version VARCHAR(36)  DEFAULT NULL );", sql.query)

    def testInitialiazeMssql(self):
        alc = Mssql(config['development']['db'])

        query = "schema_info, version, type : varchar, length : 36, default : NULL"
        sql = ChangeColumn(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("version", sql.column_name)
        self.assertEqual("ALTER TABLE schema_info ALTER COLUMN version VARCHAR(36)  DEFAULT NULL;", sql.query)

    def testExecute(self):
        alc = Sqlite(config['development']['db'])
        alc._create_schema()
        result = alc.execute_raw_query("SELECT * FROM schema_info").fetchone()
        self.assertEquals('000', result[0])

        query = "schema_info, version, type : varchar, length : 36, default : NULL"
        sql = ChangeColumn(alc, query)

        self.assertTrue(sql.execute())

if __name__ == "__main__":
    unittest.main()
